/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
/**
 * 
 */
package org.myontology.specialpages.configs;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import model.Vocabulary;

import org.myontology.controller.ResultSetHandler;
import org.myontology.controller.SesameSession;
import org.myontology.specialpages.EntryRow;
import org.openrdf.model.Value;
import org.openrdf.model.vocabulary.RDF;
import org.openrdf.model.vocabulary.RDFS;
import org.openrdf.query.BindingSet;

/**
 * @author <a href="michael.hackl@sti2.at">Michael Hackl</a>
 * Project: Myontology - www.myontology.org
 * STI
 *
 */
public class LastEdited extends AbstractSerqlSpecialPageConfig {

	private SesameSession session;
	private Set<String> versionSet;
	private Set<String> previousVersionSet;
	private List<EntryRow> list;
	
	private Map<String,Long> sortMap;
	
	public LastEdited() {
		super("Last Edited");
	}

	/* (non-Javadoc)
	 * @see org.myontology.specialpages.AbstractSpecialPageConfig#select()
	 */
	@Override
	protected List<EntryRow> select() {
		list = new ArrayList<EntryRow>();
		versionSet = new HashSet<String>();
		previousVersionSet = new HashSet<String>();
		sortMap = new HashMap<String, Long>();
		
		try {
				
			String query = "SELECT DISTINCT u,p FROM {u} <"+Vocabulary.HASPREVIOUSVERSION+"> {p}";
			logger.info("special page query: "+query);
			session.executeQuery(query, new Add2Sets());
			versionSet.removeAll(previousVersionSet);
			
			for (Iterator<String> it = versionSet.iterator();it.hasNext();) {
				String uri = it.next();
				query = "SELECT DISTINCT l,t,c,d FROM {<"+uri+">} <"+RDFS.LABEL+"> {l};<"+RDF.TYPE+"> {t};[<"+RDFS.COMMENT+"> {c}]; <"+Vocabulary.HASVERSIONDATETIME+"> {d}";
				session.executeQuery(query, new Add2List(uri));
			}
		}
		catch (Exception e) {
			logger.error(e);
		}
		
		Collections.sort(list,new Comparator<EntryRow>() {

			public int compare(EntryRow o1, EntryRow o2) {
				
				return (int)(sortMap.get(o2.getId()).longValue()-sortMap.get(o1.getId()).longValue());
			}
			
		});
		
		return list;
	}
		
	class Add2List implements ResultSetHandler {
		String uri;
		public Add2List(String u) {
			uri=u;
		}
		
		public void handleBindingSet(BindingSet set) {
			String type = set.getValue("t").stringValue();
			type=type.substring(type.lastIndexOf('#')+1);
			Value v = set.getValue("l");
			String label="NULL";
			if (v!=null)
			 label = v.stringValue();
			EntryRow row = new EntryRow(uri); 
			row.setType(type);
			
			String dateString = set.getValue("d").stringValue();
			long timeStamp = Long.parseLong(dateString);
			sortMap.put(uri, timeStamp);
			
			String comment = new Date(timeStamp)+" changed<br/>";
			comment+=set.getValue("c")==null?"-":set.getValue("c").stringValue();
			row.setComment(comment);
			
			row.setLabel(label);
			list.add(row);
		}
	}
	
	class Add2Sets implements ResultSetHandler {

		public void handleBindingSet(BindingSet set) {
			versionSet.add(set.getValue("u").stringValue());
			previousVersionSet.add(set.getValue("p").stringValue());
		}
		
	}
	
		
	}
	
	
	

